/*
 * General Djips jQuery method.
 */

/*******************************************************************************
*                           General methods                                    *
 ******************************************************************************/

/* Fade out an element before removing it */
fade_and_remove = function(selector) {
    $(selector).fadeOut('slow', function() {
        $(this).remove(); 
    });
}

/* Creates a status message */
add_status_message = function(msg, type){
    $('#content').prepend(
        '<p class="' + type + '">' + msg + '</div>'
    );
}

/* Removes all status messages */
delete_status_messages = function() {
    fade_and_remove('.info_msg');
    fade_and_remove('.error_msg');
}

jQuery.fn.log = function (msg) {
    console.log("%s: %o", msg, this);
    return this;
}
    
/*******************************************************************************
 *                          Overlay messages                                   *
 ******************************************************************************/

/* list of ids used by overlay messages */
var overlay_ids = ['modal_overlay',
                   'unmodal_overlay',
                   'modal_overlay_msg',
                   'unmodal_overlay_msg',
                   'lightbox'];


img_lightbox = function(img_url){
    $('body').append('<div id="unmodal_overlay"></div>');
    $('body').append('<div id="lightbox"><img src="' + image_url + '" /></div>');
                     
    for (id in overlay_ids) {
        $('#' + overlay_ids[id]).fadeIn();
    }
};

/* Loads a message using Ajax and displays it as overlay  */
show_overlay_msg = function(content_url, modal) {
    var msg_loaded = false;
    msg = $.ajax({url: content_url,
                  async: false,
                  success: function(){msg_loaded = true}}).responseText;

    /* If we are not able to load the message, the link will be followed */
    if (!msg_loaded) {
        return true;
    }
    
    if (modal) {
        overlay_id = 'modal_overlay';
    } else {
        overlay_id = 'unmodal_overlay';
    }
    
    $('body').append('<div id="' + overlay_id + '"></div>').
    append(msg);

    for (id in overlay_ids) {
        $('#' + overlay_ids[id]).fadeIn();
    }
    return false;
};

/* Removes all overlay elements */
close_overlay_message = function() {
    for (id in overlay_ids) {
        fade_and_remove('#' + overlay_ids[id]);
    }
}

$('#close_overlay_msg, #unmodal_overlay, #lightbox').live("click", function(){
        close_overlay_message();
        return false;        
});

/*******************************************************************************
 *                          Accordion system                                   *
 ******************************************************************************/

accordion = function() {
    id = $(this).attr('id').split('accordion_head_')[1];

    current = $('.acc_current');
    to_display = $('#accordion_body_' + id);

    if (current.attr('id') == to_display.attr('id')) {
        /* User clicked on the current 'tab' */
        return false;
    }

    current_height = current.height();
    to_display.css({'height': '0px'});
    
    current.animate({'height': '0px'},
                    function() {current.removeClass('acc_current');});
    to_display.animate({'height': current_height + 'px'},
                       function() {to_display.addClass('acc_current');});
    return false;    
}

/*******************************************************************************
 *                            Star chooser                                     *
 ******************************************************************************/

revert_stars = function(){
    star_widget = $(this);
    star_number = parseInt(star_widget.find('input').attr('value'));
    
    for (i=0; i<6; i++) {
        if (i > star_number) {
            img = images_folder + 'star_unapplied.gif';
        } else {
            img = images_folder + 'star_applied.gif';
        }        
        
        $(this).find('.mark_widget_' + i).attr('src', img);
    }
}

select_star = function(){
    star_widget = $(this).parent();
    if ($(this).hasClass('no_star')) {
        value = 0;
    } else {
        value = parseInt($(this).attr('class').split('_')[2]);
    }    

    for (i=0; i<6; i++) {
        if (i > value) {
            img = images_folder + 'star_unapplied.gif';
        } else {
            img = images_folder + 'star_hover.gif';
        }

        star_widget.find('.mark_widget_' + i).attr('src', img);
    }    
}

apply_star = function(){
    star_widget = $(this).parent();
    if ($(this).hasClass('no_star')) {
        value = 0;
    } else {
        value = parseInt($(this).attr('class').split('_')[2]);
    }    
    
    star_widget.find('input').attr('value', value);
}

make_star_widget = function(selector){
    widget = $(selector);

    widget.find('img').hover(select_star);
    widget.find('img').click(apply_star);
    widget.mouseleave(revert_stars);
}

/*******************************************************************************
 *                            Color chooser                                    *
 ******************************************************************************/

remove_check = function(selector){
    bg_color = $(selector).css('background-color');
    $(selector).css({'background': bg_color});
}

add_check = function(selector){
    bg_color = $(selector).css('background-color');
    bg = bg_color + ' url("' + images_folder +
    'check.gif") no-repeat top right';
    
    $(selector).css({'background': bg});
}

apply_check = function(){
    color_id = parseInt($(this).attr('rel').split('_')[2]);
    color_widget = $(this).parent();
    color_field = color_widget.find('input');
    old_value = parseInt(color_field.attr('value'));
    
    /* If the old value is the same than currently, the user
     * don't want to apply a color to this image */    
    if (color_id == old_value) {
        color_field.attr('value', -1);
        remove_check(this);

        return false;        
    }

    old_selected = color_widget.find('.current');    
    
    color_field.attr('value', color_id);
    remove_check(old_selected);
    $('.color_display.current').removeClass('current');

    add_check(this);
    $(this).addClass('current');

    return false;    
}

make_color_widget = function(selector){
    $(selector + ' .color_display').click(apply_check);
    add_check('.color_display.current');    
}
